set +x
EnvName={{.EnvName}}
ServiceName={{.ServiceName}}

DeployHost={{.DockerLabel}}
MainDomain={{.MainDomain}} # in nginx, the server_name
SubDomain={{.SubDomain}} # in nginx, the server_name

OutPort={{.Port}}
DockerVolumeRoot={{.DocVolume}}

BackendServiceName={{.BackendServiceName}}

DockerNetwork={{.DockerNetwork}}
DockerRepository={{.DockerRepository}}  #10.110.0.11:5000
DockerManagerHost={{.DockerManagerHost}} #g-node1  # g-node1
DockerManagerUser={{.DockerManagerUser}} #g-node1  # g-node1
DockerManagerPort={{.DockerManagerPort}} #7111
DockerBuildResourceRoot={{.DockerBuildResourceRoot}}
DockerReserveMemory=3GB

set -eu;set -o pipefail
# below lines is from jenkins var
BranchName=$Version
ForceUpdate=$should_update_nginx_config

Git='git@github.com:xxxxx/admin.git'
ProjectName=$(basename "$Git"| cut -d . -f 1)
ProjectSubFolder=dist

FrontendServerRoot=/opt/karbon_app

BuildRootDir="/opt/karbon_build"
BuildWorkDir=$BuildRootDir/$EnvName/adminportal_build
BuildTempDir=$BuildWorkDir/docker_temp_dir

DockerImageName=${EnvName}_adminportal:latest
# django static collection dst folder, will be served by nginx
NginxConfigName=admin_portal_nginx.conf
NginxTemplateFile=$DockerBuildResourceRoot/$NginxConfigName.template

changeNginxConfig () {
  sed -i 's/{.BackendServiceName}/'"$BackendServiceName"'/g' "$1"
  sed -i 's/{.SubDomain}/'"$SubDomain"'/g' "$1"
  sed -i 's/{.MainDomain}/'"$MainDomain"'/g' "$1"
}

echo "going to deploy version: $BranchName"
if [[ ! -d "$BuildRootDir" ]]; then mkdir -p "$BuildRootDir"; fi
if [[ ! -d "$BuildTempDir" ]]; then mkdir -p "$BuildTempDir"; fi

docker_vol=$DockerVolumeRoot/$ProjectSubFolder
if [[ ! -d "$docker_vol" ]]; then sudo mkdir -p "$docker_vol"; fi

cd "$BuildWorkDir"
if [[ ! -d "$ProjectName" ]]; then
  git clone "$Git"
fi

cd "$ProjectName"

git checkout -f
git fetch
if [[ "${BranchName:0:1}" == "v" ]]; then
  git checkout "$BranchName"
else
  git checkout "$BranchName"
  git reset --hard "origin/$BranchName"
fi

# for npm
export PATH=$PATH:/usr/local/node/bin
export http_proxy="http://127.0.0.1:8118";export https_proxy="http://127.0.0.1:8118";
npm install
npm run build:release

# copy build out content
sudo cp -a $ProjectSubFolder/* $docker_vol/

# check if service is already exists
if ssh -p $DockerManagerPort $DockerManagerUser@$DockerManagerHost sudo docker service ls |grep $ServiceName; then
  if [[ "$ForceUpdate" != "yes" ]]; then
    echo "frontend files updated"
    exit 0
  fi
fi

# copy nginx config file to docker build folder
if [[ ${#BuildTempDir} -gt 20 ]];then
	rm -rf ${BuildTempDir:?}/*
else
	echo "to delete '$BuildTempDir' is dangerous"
    exit 1
fi

cp $NginxTemplateFile "$BuildTempDir/$NginxConfigName"
# change NginxConfig content
changeNginxConfig "$BuildTempDir/$NginxConfigName"

# build docker image
echo "=== building docker: $DockerImageName"
sudo docker build --no-cache --rm --force-rm -t "$DockerImageName" -f- "$BuildTempDir" <<EOF
FROM nginx:1.17.9
COPY ./$NginxConfigName /etc/nginx/conf.d/
EXPOSE 80
EOF

echo "=== finish build of docker: $DockerImageName"

# push to image to the registry
sudo docker tag "$DockerImageName" "$DockerRepository/$DockerImageName"
sudo docker push "$DockerRepository/$DockerImageName"
sudo docker image rm "$DockerImageName"

if [[ $DeployHost == "" ]]; then
  ssh -p $DockerManagerPort $DockerManagerUser@$DockerManagerHost \
    sudo docker service rm $ServiceName\; \
    sudo docker service create --reserve-memory=$DockerReserveMemory \
    --name $ServiceName \
    --network $DockerNetwork \
    --publish $OutPort:80 \
    --mount type=bind,source=$DockerVolumeRoot/$ProjectSubFolder,destination="$FrontendServerRoot/$ProjectName/$ProjectSubFolder" \
    "$DockerRepository/$DockerImageName"
else
  ssh -p $DockerManagerPort $DockerManagerUser@$DockerManagerHost \
    sudo docker service rm $ServiceName\; \
    sudo docker service create --reserve-memory=$DockerReserveMemory \
    --name $ServiceName \
    --network $DockerNetwork \
    --publish $OutPort:80 \
    --mount type=bind,source=$DockerVolumeRoot/$ProjectSubFolder,destination="$FrontendServerRoot/$ProjectName/$ProjectSubFolder" \
    --constraint node.labels.khost==$DeployHost \
    "$DockerRepository/$DockerImageName"
fi

